package itcz.example.pattern.structure_type.flyweight;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName: BoxFactory
 * @Author: ITCZ
 * @Date: 2025-06-27
 * @Description: 享元工厂（Flyweight Factory）角色
 */
public class BoxFactory {
    private Map<String, Abstractbox> map;

    private BoxFactory() {
        map = new HashMap<String, Abstractbox>();
        map.put("I", new IBox());
        map.put("L", new LBox());
        map.put("O", new OBox());
    }

    public static BoxFactory getInstance() {
        return BoxFactory.SingletonHolder.INSTANCE;
    }

    // 静态内部类，在jvm加载外部类时不会加载内部类，只有内部类的属性/方法被调用时才会被加载，
    // 使用static 修饰 保证只被实例化一次
    private static class SingletonHolder {
        private static final BoxFactory INSTANCE = new BoxFactory();
    }

    public Abstractbox getBox(String type) {
        return map.get(type);
    }
}
